/**
 * @author Fang Lu, fanglupku@gmail.com
 * 2012-2-7
 */
package com.autotag.data.extract;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.autotag.dao.AbstractProviderDao;
import com.autotag.entity.AbstractProvider;

import fanglu.util.FileDealer;
import fanglu.util.SQLDealer;

public class ProviderExtractor {
	private static Pattern providerListPattern = Pattern.compile(
			"<div id=\"column.\">.*?</div>", Pattern.DOTALL
					| Pattern.CASE_INSENSITIVE);
	private static Pattern abstractProviderPattern = Pattern.compile(
			"href=\"(.+?)\">(.+?)\\s+?\\((\\d+?)\\)</a></li>",
			Pattern.CASE_INSENSITIVE);

	public static List<AbstractProvider> extractAbstractProviderList(
			String pageContent) {
		ArrayList<AbstractProvider> resultList = null;
		Matcher matcher = providerListPattern.matcher(pageContent);
		while (matcher.find()) {
			if (resultList == null) {
				resultList = new ArrayList<AbstractProvider>();
			}
			String thisDiv = new String(pageContent.substring(matcher.start(),
					matcher.end()));
			Matcher subMatcher = abstractProviderPattern.matcher(thisDiv);
			while (subMatcher.find()) {
				String thisProviderLine = new String(thisDiv.substring(
						subMatcher.start(), subMatcher.end()));
				try {
					Integer serviceNum = Integer.parseInt(subMatcher.group(3));
					String ref = subMatcher.group(1);
					String name = subMatcher.group(2);
					AbstractProvider thisAbstractProvider = new AbstractProvider(
							-1, name, ref, serviceNum,
							AbstractProvider.ABSTRACT_PROVIDER);
					resultList.add(thisAbstractProvider);
				} catch (Exception e) {
					System.out.println("error:" + thisProviderLine);
				}
			}
		}
		return resultList;
	}

	public static boolean insertAbstractProviderList(Connection conn,
			List<AbstractProvider> providerList) {
		boolean result = true;
		Iterator<AbstractProvider> itor = providerList.iterator();
		while (itor.hasNext()) {
			result = AbstractProviderDao.insertProvider(conn, itor.next()) & result;
		}
		return result;
	}

	public static boolean processAbstractProviderListFromFile(Connection conn,
			String fileName, String encoding) {
		boolean result = true;
		try {
			String content = FileDealer.getFileContent(fileName, encoding);
			List<AbstractProvider> pList = extractAbstractProviderList(content);
			result = insertAbstractProviderList(conn, pList);
		} catch (IOException e) {
			e.printStackTrace();
			result = false;
		}
		return result;
	}

	public static void processAllProviderListFile(String baseDir,
			String encoding, String dbURL, String user, String pwd) {
		char l = 'a';
		Connection conn = SQLDealer.getDbConnection(dbURL, user, pwd);
		for (int i = 0; i < 27; ++i) {
			String thisFileName = baseDir + "/" + ((char) (l + i)) + ".htm";
			if (i == 26) {
				thisFileName = baseDir + "/other.htm";
			}
			boolean thisResult = processAbstractProviderListFromFile(conn,
					thisFileName, encoding);
			if (thisResult) {
				System.out.println("process OK! " + thisFileName);
			} else {
				System.out.println("process Failed! " + thisFileName);
			}
		}
	}

	public static void main(String args[]) {
		processAllProviderListFile("seekda_data/provider", "utf8",
				"jdbc:mysql://localhost:3306/autotag", "root", "ftc86524");
	}
}
